package DAO;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.ietf.jgss.Oid;

import DTO.DatabaseDTO;



public class DatabaseDAO{
	private String _DatabasePath;
	public DatabaseDAO(String databasePath)
	{
		_DatabasePath = databasePath;
	}
	
	public void save(DatabaseDTO databaseDTO) throws Exception
	{
		try
		{
			File f = new File(_DatabasePath);
			if(f.exists())
				f.delete();
			FileOutputStream fos = new FileOutputStream(_DatabasePath);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(databaseDTO);	
			oos.flush();
			fos.close();
			oos.close();
		}catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
	}
	
	public  DatabaseDTO getDatabase() throws Exception
	{
		try
		{
			FileInputStream fis = new FileInputStream(_DatabasePath);
			ObjectInputStream ois = new ObjectInputStream(fis);
			DatabaseDTO result = (DatabaseDTO)ois.readObject(); 
			fis.close();
			ois.close();
			return result;
		}catch (EOFException e) {
			// TODO: handle exception
			return null;
		}catch (Exception e) {
			// TODO: handle exception
			throw e;
		}finally
		{
			
		}
	}
}
